import FB from "./FB";

let g_leaderboard = {}

//排行榜封装
export default class fbRanking
{
    //{
    //  name:<排行名称>
    //  success:<成功回调>
    //  fail:<失败回调>
    //}
    public static FriendsRanking(
        obj:any
        )
    { 
        if(!FB.InFB) return

        var loadfriend
        if(g_leaderboard[obj.name])
        { 
            loadfriend = g_leaderboard[obj.name].getConnectedPlayerEntriesAsync();
        }
        else
        {
            var loadrk = FB.getLeaderboardAsync(obj.name) 
            loadfriend = loadrk.then(function(leaderboard) {
                g_leaderboard[obj.name] = leaderboard
                return leaderboard.getConnectedPlayerEntriesAsync();
            })
            .catch(function(e) {
                cc.log("FriendRanking#1",e);
                if(obj.fail) obj.fail()
            });
        }


        loadfriend.then(function(entries) {
            /*
          console.log(entries.length); // 10
          console.log(entries[0].getRank()); // 1
          console.log(entries[0].getScore()); // 42
          console.log(entries[1].getRank()); // 2
          console.log(entries[1].getScore()); // 40
           */
            if(obj.success) obj.success(entries)
        })
        .catch(function(e) {
            cc.log("FriendRanking#2",e);
            if(obj.fail) obj.fail()
        });
    }

    // return leaderboard.getEntriesAsync();

    //获取世界排名
    //{
    //  name:<排行名称>
    //  success:<成功回调>
    //  fail:<失败回调>
    //}
    public static WorldRanking(
        obj:any
        )
    { 
        if(!FB.InFB) return

        var loadfriend
        if(g_leaderboard[obj.name])
        { 
            loadfriend = g_leaderboard[obj.name].getEntriesAsync(100);
        }
        else
        {
            var loadrk = FB.getLeaderboardAsync(obj.name) 
            loadfriend = loadrk.then((leaderboard) =>{
                console.log("WorldRanking Get Leaderboard Success.")
                g_leaderboard[obj.name] = leaderboard
                return leaderboard.getEntriesAsync(100);
            })
            .catch((e)=> {
                cc.log("WorldRanking#1",e);
                if(obj.fail) obj.fail()
            });
        }


        loadfriend.then((entries)=> {
            console.log("WorldRanking Get List Success.")
            if(obj.success) obj.success(entries)
        })
        .catch((e)=> {
            cc.log("WorldRanking#2",e);
            if(obj.fail) obj.fail()
        });
    }




    //设置自己的成绩
    //{
    //  name:<排行名称>
    //  score:<成绩>
    //}
    public static SetScore(obj:any)
    {
        if(g_leaderboard[obj.name])
        { 
            g_leaderboard[obj.name].setScoreAsync(obj.score)
        }
        else
        {
            FB.getLeaderboardAsync(obj.name)
            .then(function(leaderboard) {
                g_leaderboard[obj.name] = leaderboard
                g_leaderboard[obj.name].setScoreAsync(obj.score)
            })
            .catch(function(e) {
                cc.log("SetScore#1",e); 
            });
        }
    }
}


/*
.getPlayer().getName()
        score_leaderboard.<context_id>
        */ 